<?php

namespace app\model\v1\recovery;

use app\model\system\admin\SystemAdmin;
use crmeb\traits\ModelTrait;
use think\facade\Db;
use think\Model;


class ConfigBanner extends Model
{

    const SHOW_TYPE_LINE = 1;  //1 显示
    const SHOW_TYPE_UNLINE = 2;  //2 不显示


    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];

    protected $table = 'eb_config_banner';


    public function admin()
    {
        return $this->hasOne(SystemAdmin::class, 'id', 'create_id');
    }



    /**
     * 添加banner
     * @param $param
     * @return int|string
     */
    public static function createConfigLunbotu($param = [])
    {
        $add = [
            "create_id"     => $param["admin_id"],  //创建人id
            "show_position" => $param["show_position"] ?? '', //展示模块
            "type"          => $param["type"] ?? '',  //细分分类
            "client_type"   => $param["client_type"] ?? '',  //客户端类型
            "show_type"     => $param["show_type"] ?? 1,  //是否显示 1显示 2不显示
            "banner_img"    => $param["banner_img"] ?? '',  //图片路径
            "link_content"  => $param["link_content"] ?? '',  //链接url
            "sort"          => $param["sort"] ?? '',  //排序
            "link_type"     => $param["link_type"] ?? '',  //跳转类型 0无跳转链接 1链接 2商品 3.资讯
            "panner_title"  => $param["panner_title"] ?? '',  //标题
            "banner_title"  => $param["banner_title"] ?? '',  //金刚区标题
            "cate_id"       => $param["cate_id"] ?? '',
            "create_time"   => time(),
        ];

        return self::insert($add);
    }



    /**
     * 获取banner列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getBannerList($param = [])
    {
        $order = ["id"=>"desc"];
        //按照创建时间排序
        if(!empty($param["order_ctime"])){
            if(!empty($param["order_ctime_sc"])){
                $order = ["id"=>"desc", "create_time"=> $param["order_ctime_sc"]];
            }else{
                //默认按照字段降序排序
                $order = ["id"=>"desc", "create_time"=>"desc"];
            }
        }

        $data["data"] = self::field ("*,FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text 
            ")
            ->with([
                "admin" => function ($query) {
                    $query->field("id,account,real_name");
                }
            ])
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                //根据关键字搜索
                if(!empty($param["word"])) $query->where("panner_title",'like', '%'.$param["word"].'%');
                //根据展示模块搜索
                if(!empty($param["show_position"])) $query->where("show_position", $param["show_position"]);
                //根据类型搜索
                if(!empty($param["type"])) $query->where("type", $param["type"]);
            })
            ->order($order)
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("id,create_id,show_position,type,client_type,show_type,banner_img,link_content,panner_title,status,create_time")
            ->with([
                "admin" => function ($query) {
                    $query->field("id,account,real_name");
                }
            ])
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("panner_title",'like', '%'.$param["word"].'%');
                if(!empty($param["show_position"])) $query->where("show_position", $param["show_position"]);
                if(!empty($param["type"])) $query->where("type", $param["type"]);
            })
            ->count();

        return $data;
    }


    /**
     * banner管理列表 - 获取展示位置模块列表
     * @param $param
     * @return array[]
     */
    public static function getShowPositionList($param = [])
    {
        return [
            [
                "id" => 1,
                "name" => '首页头图',
            ],
            [
                "id" => 2,
                "name" => '首页金刚区',
            ],
            [
                "id" => 3,
                "name" => '首页轮播图',
            ],
            [
                "id" => 4,
                "name" => '室内快修',
            ],
            [
                "id" => 5,
                "name" => '店铺轮播图',
            ],
            [
                "id" => 6,
                "name" => '闲置轮播图',
            ],
            [
                "id" => 7,
                "name" => '求职者身份企业招聘头图',
            ],
            [
                "id" => 8,
                "name" => '求职者身份店铺招聘头图',
            ],
            [
                "id" => 9,
                "name" => '企业身份人才轮播图',
            ],
            [
                "id" => 10,
                "name" => '会员中心宣传图',
            ],
            [
                "id" => 12,
                "name" => '店铺闲置分类',
            ],
            [
                "id" => 13,
                "name" => '店铺转让分类',
            ],
            [
                "id" => 14,
                "name" => '首页宣传图',
            ],
            [
                "id" => 15,
                "name" => '首页滑块图',
            ],
            [
                "id" => 20,
                "name" => '启动图',
            ],
            [
                "id" => 22,
                "name" => '百街水站LOGO',
            ],
            [
                "id" => 23,
                "name" => '百街超市LOGO',
            ],
            [
                "id" => 30,
                "name" => '礼品卡首页轮播图',
            ],
            [
                "id" => 31,
                "name" => '礼品卡首页提示图片',
            ]
        ];
    }




    /**
     * 获取banner列表 - 编辑
     * @param $param
     * @return ConfigBanner
     */
    public static function updateBanner($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["show_position"])) $update["show_position"] = $param["show_position"];  //展示模块位置
        if(!empty($param["type"]))          $update["type"] = $param["type"]; //细分分类
        if(!empty($param["client_type"]))   $update["client_type"] = $param["client_type"]; //客户端类型
        if(!empty($param["show_type"]))     $update["show_type"] = $param["show_type"]; //是否显示 1显示 2不显示
        if(!empty($param["banner_img"]))    $update["banner_img"] = $param["banner_img"]; //图片路径
        if(!empty($param["link_content"]))  $update["link_content"] = $param["link_content"]; //链接url
        if(!empty($param["sort"]))          $update["sort"] = $param["sort"]; //排序
        if(!empty($param["status"]))        $update["status"] = $param["status"]; //1 启用  2禁用
        if(!empty($param["link_type"]))     $update["link_type"] = $param["link_type"]; //跳转类型 0无跳转链接 1链接 2商品 3.资讯
        if(!empty($param["panner_title"]))  $update["panner_title"] = $param["panner_title"]; //标题
        if(!empty($param["banner_title"]))  $update["banner_title"] = $param["banner_title"]; //金刚区标题
        if(!empty($param["cate_id"]))       $update["cate_id"] = $param["cate_id"];

        return self::where("id", $param['id'])->update($update);
    }


    /**
     * banner管理列表 - 详情
     * @param $param
     * @return array|bool|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getBannerDetail($param = [])
    {
        // 根据用户id和后台页面发送过来的数据，获取banner的详情
        $detail = self::field ("id,create_id,show_position,type,client_type,show_type,a.banner_img,a.link_content,a.panner_title,a.status,a.create_time,
            case when a.status=1 then '启用' 
            when a.status=2 then '禁用' 
            end as status_text 
            ")
            ->with([
                "admin" => function ($query) {
                    $query->field("id,account,real_name");
                }
            ])
            ->where("a.id", $param["id"])
            ->find();

        return $detail;
    }


    /**
     * banner管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableBannerStatus($param = [])
    {
        $status = ConfigBanner::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        ConfigBanner::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }


}